import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val=val;
        }
    }

    public TreeNode createTree() {
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');

        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        E.right=H;

        return A;
    }

    public static int i=0;
    public TreeNode createTree(String str) {
        TreeNode root=null;
        if(str.charAt(i)!='#') {
            root=new TreeNode(str.charAt(i));
            i++;
            root.left=createTree(str);
            root.right=createTree(str);
        }else {
            //空树
            i++;
        }
        return root;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if(root==null) return;
        System.out.println(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root==null) return;
        inOrder(root.left);
        System.out.println(root.val);
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if(root==null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.val);
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null) return;
        queue.offer(root);
        while(!queue.isEmpty()){
            //将当前节点保存在cur中
            TreeNode cur=queue.poll();
            //打印
            System.out.println(cur.val+" ");

            //压入左右子节点
            if(cur.left!=null)queue.offer(cur.left);
            if(cur.right!=null)queue.offer(cur.right);
        }
        System.out.println();
    }

    /**
     * 层序遍历
     * @param root
     * @return 返回的是一个二维数组

    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> ret=new ArrayList<>();
        if(root==null) return ret;

        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){
            int size=queue.size();
            List<Integer> list=new ArrayList<>();//每一层

            while(size>0) {
                //将当前节点保存在cur中
                TreeNode cur=queue.poll();
                list.add(cur.val);
                //压入左右子节点
                if(cur.left!=null)queue.offer(cur.left);
                if(cur.right!=null)queue.offer(cur.right);
                size--;
            }
            ret.add(list);
        }
        return ret;
    }*/

    //判断一棵树是否为完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if(root==null) return true;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){
            //当队列非空时，弹出队头元素给cur
            TreeNode cur=queue.poll();

            if(cur==null) break;
            //若cur不为null，将cur的左右节点都放进来，null也放
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        //此时的cur为空，开始判断此时队列的节点是否有非空的节点
        while(!queue.isEmpty()){
            TreeNode node=queue.peek();
            if(node!=null) return false;
            else queue.poll();
        }
        return true;
    }


    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null)return null;
        if(root==p || root==q) return root;

        TreeNode leftTree =lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree=lowestCommonAncestor(root.right,p,q);

        // leftTree和rightTree的返回值有下面几种情况：
        if(leftTree!=null && rightTree!=null)return root;//1. p,q一个在左边一个在右边
        else if(leftTree!=null) return leftTree;//2. 都在左边
        else return rightTree;//3. 都在右边
    }



    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;

        Stack<TreeNode> stackP=new Stack<>();
        getPath(root,p,stackP);
        Stack<TreeNode> stackQ=new Stack<>();
        getPath(root,q,stackQ);

        //上述代码执行完成，对应栈中存储了指定路径上的所有节点

        int sizeP=stackP.size();
        int sizeQ=stackQ.size();

        if(sizeP>sizeQ) {
            int size=sizeP-sizeQ;
            while(size!=0) {
                stackP.pop();
                size--;
            }
        }else {
            int size=sizeQ-sizeP;
            while(size!=0) {
                stackQ.pop();
                size--;
            }
        }
        //出if语句后，两栈的节点个数相等
        while(!stackP.isEmpty()&& !stackQ.isEmpty()) {
            if(stackP.peek() == stackQ.peek()) {
                return stackP.peek();
            }else {
                stackP.pop();
                stackQ.pop();
            }
        }
        return null;
    }

    /**
     *   该函数用于存储根节点到指定节点的路径，存储到一个栈中
     * @param root
     * @param node
     * @param stack
     * @return
     */
    public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack){
        /*如何确定你存入的路径就是该节点的呢？
        以前序遍历的方式遍历这棵树，当走到一个节点时，该节点本身不是，且左子树和右子树都没有时，
        就可以确定这个节点不应存在路径当中，应当从栈中弹出该节点
         */
        if(root==null)return false;
        stack.push(root);
        if(root==node)return true;
        //前序遍历
        boolean flg=getPath(root.left,node,stack);
        if(flg) return true;

        //左边没有，去右边找
        boolean flg2=getPath(root.right,node,stack);
        if(flg2) return true;

        //左右遍历都返回了false，那么弹出该节点
        stack.pop();
        return false;
    }



//    非递归前序遍历的实现
    public void preOrderNot(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;

        while(cur!=null || !stack.isEmpty()) {
            while(cur!=null) {
                stack.push(cur);
                System.out.print(cur.val+" ");
                cur=cur.left;
            }

            //此时cur为空，也就是遍历到了这棵树最左边的叶子节点的左子节点，为null
            TreeNode top=stack.pop();
            cur=top.right;
        }
    }



//    非递归中序遍历
    public void inOrderNot(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;

        while(cur!=null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur=cur.left;
            }

            TreeNode top=stack.pop();
            System.out.print(top.val+" ");
            cur=top.right;
        }

    }

    //    非递归后序遍历
    public void postOrderNot(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode prev=null;
        while(cur!=null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.peek();
            if(top.right==null || top.right==prev) {
                //右节点为空，那么可以弹出该节点
                stack.pop();
                System.out.print(top.val+"");
                prev=top;
            }else {
                //右节点不为空，那么当前节点还不能弹出
                cur=top.right;
            }
        }
    }
}
